//
//  HSOpenSecondLevelVC.m
//  hsdyOrderSystem
//
//  Created by 赖国锋 on 2017/11/27.
//  Copyright © 2017年 赖国锋. All rights reserved.
//

#import "HSOpenSecondLevelVC.h"
#import "UITextField+SMKLimit.h"
#import "YJGCDTimer.h"

@interface HSOpenSecondLevelVC ()
@property (weak, nonatomic) IBOutlet UITextField *phoneTx;
@property (weak, nonatomic) IBOutlet UITextField *verifyTX;
@property (weak, nonatomic) IBOutlet UIButton *getVerifyBtn;
@property (weak, nonatomic) IBOutlet UILabel *tipLabel;
@property(nonatomic,strong)YJGCDTimer *timer;
@end

@implementation HSOpenSecondLevelVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title =@"开二级账号";
    self.view.backgroundColor = LCOLOROFBACK;
    _timer = [[YJGCDTimer alloc] init];
    self.tipLabel.numberOfLines = 0;
    self.tipLabel.text = @"温馨提示：\n1.可为服务员等开通二级账号，登录该店共同管理\n2.二级账号仅本账户可开通\n3.二级账号初始密码为123456";
}

//获取验证码
- (IBAction)getVerifyBtnClick:(id)sender {
    if (self.phoneTx.text.length != 11 ) {
        [SOAutoHideMessageView showMessage:@"请输入正确的手机号码" inView:self.view];
        return;
    }
    [self.phoneTx resignFirstResponder];
    [self.verifyTX resignFirstResponder];
    WEAKSELF
    [self showLoadingWithClear:YES];
    [XFPostRequest postUserGetVerifyCodeByUsername:self.phoneTx.text success:^(id responseObject) {
        [weakSelf hideLoading];
        LGFLog(@"responseObject = %@",responseObject);
        [SOAutoHideMessageView showMessage:responseObject[@"msg"] inView:weakSelf.view];
        if ([responseObject[@"status"] intValue] == 200) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.75 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf startTimer];//倒计时
            });
        }
    } failure:^(NSError *error) {
        LGFLog(@"%@",error);
        [weakSelf hideLoading];
        [SOAutoHideMessageView showMessage:LNETWORKERROR inView:self.view];
    }];
}

- (void)startTimer{
    self.getVerifyBtn.enabled = NO;
    WEAKSELF
    __block int count = 61;
    [_timer startWithTimeInterval:1.0 Handler:^{
        NSString *title = [NSString stringWithFormat:@"%d秒",--count];
        [weakSelf.getVerifyBtn setTitle:title forState:UIControlStateDisabled];
        if (count == 0){
            [weakSelf.getVerifyBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
            weakSelf.getVerifyBtn.enabled = YES;
            [weakSelf.timer cancel];
        }
    }];
}

-(void)dealloc{
    [_timer cancel];
}

//确认并开通
- (IBAction)sureAndOpenBtnClick:(id)sender {
    WEAKSELF
    [self showLoadingWithClear:YES];
    [XFPostRequest postaddWaiterUsername:self.phoneTx.text sms:self.verifyTX.text success:^(id responseObject) {
        [weakSelf hideLoading];
        if ([[responseObject safeJsonObjectForKey:@"status"] integerValue] == 200
            ) {
            LGFLog(@"responseObject = %@",responseObject);
            [SOAutoHideMessageView showMessage:[responseObject safeJsonObjectForKey:@"msg"] inView:weakSelf.view];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.75 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf.navigationController popViewControllerAnimated:YES];
            });
        }
    } failure:^(NSError *error) {
        LGFLog(@"error = %@",error);
        [weakSelf hideLoading];
        [SOAutoHideMessageView showMessage:LNETWORKERROR inView:weakSelf.view];
    }];
}

@end
